/**
 * 后台功能
 * */


(function () {
})();


//获取功能列表
function getList(){
    var formData = {
        start: pages.start, limit: pages.limit,
    };
    $.ajax({
        type: apis.emojiIndex.method,
        url: apis.emojiIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', i, tbody = $('tbody');
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                h = '';
                h += '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}
//HTML
function getHtml(d){
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        //h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>';
        if (d[i].link == '') {
            h += '-';
        } else {
            h += '<img src="' + d[i].link + '" width="50"/>';
        }
        h += '</td>';
        h += '<td>' + d[i].uptime + '</td>';
        h += '<td class="f-14">';
        h += '<a href="javascript:;" onclick="lay_edit(\'修改\',\'cate-edit.html\', ' + d[i].id + ');">';
        h += '编辑';
        h += '</a>';
        h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].title + '"/>';
        h += '<input type="hidden" id="img' + d[i].id + '" value="' + d[i].link + '"/>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
        h += '删除';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    var tt = $('#title' + id).val();
    var link = $('#link' + id).val();
    tt = tt ? tt : '';
    url += '&title=' + escape(tt);
    url += '&link=' + link;
    layer_show(title,url,w,450);
}


//详情
function getOne(){
    if (!id) { return; }
    $('#title')[0].value = title;
    if (img != '') {
        var h = '';
        h += '<img src="/' + img + '"/>';
        h += '<input type="hidden" id="img" value="' + img + '"/>';
        $('#bigShow').html(h);
    }
}


//显示图片
function showImg(e){
    var h = '', i;
    h += '<img src="' + e.domain + e.img + '"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h).height(e.h);
}


//保存
function modify(){
    var formData = {
        id: id,
        title: $('#title').val(),
        img: $('#img').val(),
    };
    console.log(formData);
    //return
    $.ajax({
        type: apis.emojiModify.method,
        url: apis.emojiModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layerMsgErr(res.msg);
                return
            }
            layerMsgOk('操作成功');
            setTimeout(function () {
                //top.location.href = '/admin/views/cate.html';
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//删除
function lay_del(v){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.emojiDel.method,
            url: apis.emojiDel.url,
            data: { id: v },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layerMsgErr(res.msg); return
                }
                layerMsgOk('删除成功!');
                setTimeout(function(){
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}